//
//  RetryViewController.swift
//  testRxSwift
//
//  Created by 火星人 on 2017/7/25.
//  Copyright © 2017年 火星人. All rights reserved.
//

import UIKit
import RxSwift

class RetryViewController: BaseViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        self.view.addTapWithHandle { (tap) in
            
            let bag = DisposeBag()
            
            enum TestError: Error {
                case test
            }
            
            var count = 1
            
            let sequence = Observable<Int>.create({ (obs) -> Disposable in
                
                obs.onNext(1)
                obs.onNext(2)
                
                if count < 4 {
                    // 模拟发生异常
                    obs.onError(TestError.test)
                    print("Error encountered")
                    count += 1
                }
                
                obs.onNext(3)
                obs.onNext(4)
                
                obs.onCompleted()
                
                return Disposables.create()
                
            })
            
            sequence
                .retry(5)
                .subscribe({ (eve) in
                    print(eve)
                })
                .disposed(by: bag)
            
        }
    }

}
